<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
		<title>Google map api demo</title>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&region=zh-CN"></script>
		<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
		<style type="text/css">
		  html { height: 100% }
		  body { height: 100%; margin: 0px; padding: 0px }
		  #map { height: 100%;width:100%}
		</style> 
	</head>
	<body>
		<div id="map" style="width: 100%; height: 100%"></div>
		<script type="text/javascript">
		//<![CDATA[
		(function($){
			var mapBase={
				initLocation:null,
				beijing:new google.maps.LatLng(116.404,39.915),
				browserSupport:false,
				map:null,
				tipBox:new google.maps.InfoWindow(),
				showMsg:function(msg){
					this.tipBox.setContent(msg);
					this.tipBox.setPosition(this.initLocation);
					this.tipBox.open(this.map);
				},
				handleNoGeolocation:function(browserSupport,cbk){
					this.initLocation=this.beijing;
					if (browserSupport == true) {
						this.showMsg("Error: The Geolocation service failed.Use Beijing as the default location");
					} else {
						this.showMsg("Error: Your browser doesn't support geolocation. Are you in Beijing?");
					}
					this.updateCenter();
					if(cbk){
						cbk(this);
					};

				},
				updateCenter:function(){
					this.map.setCenter(this.initLocation);
					return this;
				}
			};
			
			var initMap=function(mapOpts,cbk){
				//map options
				mapOpts=$.extend({
					zoom:10,
					mapTypeId:google.maps.MapTypeId.ROADMAP
				},mapOpts||{});
				
				//map instance
				mapBase.map=new google.maps.Map($("#map")[0],mapOpts);
				//map init location detect
				
				//try w3c geolocation method,preferred
				if(navigator.geolocation){
					mapBase.browserSupport=true;
					navigator.geolocation.getCurrentPosition(function(position){
						mapBase.initLocation=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
						mapBase.updateCenter().showMsg("Location found using W3C standard");
						if(cbk){
							cbk(mapBase);
						};
					},function(){
						mapBase.handleNoGeolocation(true,cbk);
					});
				}else if(google.gears){
					//try google gears geolocation
					mapBase.browserSupport=true;
					var geo=google.gears.factory.create("beta.geolocation");
					geo.getCurrentPosition(function(){
						mapBase.initLocation=new google.maps.LatLng(position.latitude,position.longitude);
						mapBase.updateCenter().showMsg("Location found using Google gears");
						if(cbk){
							cbk(mapBase);
						};
					},function(){
						mapBase.handleNoGeolocation(true,cbk);
					});
				}else{
					//browser doesn't support geolocation
					mapBase.browserSupport=false;
					mapBase.handleNoGeolocation(false,cbk);
				};
				
				/*
				if(cbk){
					cbk(mapBase);
				};
				*/
			};
			
			//load data
			var loadData=function(mapBos){
				var url="http://119.88.56.78:9090/web/getLoad.do",markers=[];
				$.getScript(url+"?t="+new Date().getTime(),function(){
					//mapBos.initLocation=new google.maps.LatLng(QeeTuan_Data.cpointX,QeeTuan_Data.cpointY);
					//mapBos.updateCenter().showMsg(QeeTuan_Data.city);
					
					var geocoder=new google.maps.Geocoder(),i=0;
					//markers
					var doGeocode=function(i){
						if(i==QeeTuan_Data.items.length) return;
						geocoder.geocode({
							address:QeeTuan_Data.items[i].addr
						},function(r,status){
							if(status==google.maps.GeocoderStatus.OK){
								markers.push(new google.maps.Marker({
									map:mapBos.map,
									position:r[0].geometry.location
								}));
							};
							doGeocode(++i);
						});
					};
					doGeocode(0);
				});
			};
			
			$(document).ready(function(){
				initMap(null,loadData);
			});
			
		})(jQuery);	
		//]]>
		</script> 
	</body>
</html>
